package ferp.android.views.table.element.trick;

import android.content.Context;
import android.graphics.Rect;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import ferp.android.GUI;
import ferp.android.R;
import ferp.android.views.table.TableView;
import ferp.android.views.table.element.Element;

/**
* User: user
* Date: 6/22/12 7:40 PM
*/
public class TrickCounter extends Element<TextSwitcher>
{
  public TrickCounter(Context context, TableView parent) {super(context, parent);}

  public void reset() {set(0);}
  public void set(int count) {view.setCurrentText(String.valueOf(count));}
  public void update(int count) {view.setText(String.valueOf(count));}

  @Override
  protected final TextSwitcher create(final Context context, TableView parent)
  {
    TextSwitcher switcher = new TextSwitcher(context);

    switcher.setFactory(new ViewSwitcher.ViewFactory()
    {
      @Override
      public View makeView()
      {
        TextView tv = new TextView(context);
        FrameLayout.LayoutParams flp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT, Gravity.CENTER);

        tv.setLayoutParams(flp);
        tv.setGravity(Gravity.CENTER);
        tv.setTypeface(GUI.Font.BOLD);
        tv.setTextColor(GUI.Color.NORMAL);
        tv.setText("", TextView.BufferType.SPANNABLE);

        return tv;
      }
    });

    switcher.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.slide_in_up));
    switcher.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.slide_out_up));
    switcher.setBackgroundResource(R.drawable.trick_counter_bg);
    switcher.setForeground(context.getResources().getDrawable(R.drawable.trick_counter_fg));
    switcher.setCurrentText("0");

    return switcher;
  }

  @Override
  protected void onBeforeLayout(Rect area)
  {
    // scale the text in text switcher according to it's height
    int ts = Math.round(area.height() / 2.7f);

    ((TextView)view.getChildAt(0)).setTextSize(TypedValue.COMPLEX_UNIT_PX, ts);
    ((TextView)view.getChildAt(1)).setTextSize(TypedValue.COMPLEX_UNIT_PX, ts);
  }
}
